<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Bet_tournament_entity{
	var $id = 0;
	var $event_id = 0;
	var $user_id = 0;
	var $stake = 0;
	var $status = "";
	var $result = "";
	var $bet_time = 0;
	var $tournament_id = 0;
	var $point_get = 0;
	var $odds = 0;
	
	function __destruct() {}
	function __construct() {}
    
	function init($cmd, $input)
	{
		$ci=& get_instance();
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_BET_TOURNAMENT)->result();
		}
		else
		{
			return false;
		}
		
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->event_id = $result[0]->event_id;
			$this->user_id = $result[0]->user_id;
			$this->stake = $result[0]->stake;
			$this->status = $result[0]->status;
			$this->result = $result[0]->result;
			$this->bet_time = $result[0]->bet_time;
			$this->tournament_id = $result[0]->tournament_id;
			$this->point_get = $result[0]->point_get;
			$this->odds = $result[0]->odds;
			
			return $this;
		}
		return false;
	}
	
	function setEventId($input){ $this->event_id=$input; }
	function setUserId($input){ $this->user_id=$input;}
	function setStake($input){ $this->stake=$input; }
	function setStatus($input){ $this->status=$input; }
	function setResult($input){ $this->result=$input; }
	function setBetTime($input){ $this->bet_time=$input; }
	function setTournamentId($input){ $this->tournament_id=$input; }
	function setPointGet($input){ $this->point_get=$input; }
	function setOdds($input){ $this->odds= $input; }
	
	function getId(){ return $this->id; }
	function getEventId(){ return $this->event_id; }
	function getUserId(){ return $this->user_id; }
	function getStake(){ return $this->stake; }
	function getStatus(){ return $this->status; }
	function getResult(){ return $this->result; }
	function getBetTime(){ return $this->bet_time; }
	function getTournamentId(){ return $this->tournament_id; }
	function getPointGet(){ return $this->point_get; }
	function getOdds(){ return $this->odds; }
	
	function update()
	{
		$ci=& get_instance();
		 
		$ci->db->set('event_id', $this->event_id);
		$ci->db->set('user_id', $this->user_id);
		$ci->db->set('stake', $this->stake);
		$ci->db->set('status', $this->status);
		$ci->db->set('result', $this->result);
		$ci->db->set('bet_time', $this->bet_time);
		$ci->db->set('tournament_id', $this->tournament_id);
		$ci->db->set('point_get', $this->point_get);
		$ci->db->set('odds', $this->odds );
		
		$ci->db->where('id', $this->id)->update(TBL_BET_TOURNAMENT);
	}
	
	function getTotalStakeUserBettedCurrently($eventId, $userId){
		$ci=& get_instance();
		$sum = $ci->db->query("SELECT SUM(stake) AS total FROM ".TBL_BET_TOURNAMENT." WHERE event_id=$eventId AND user_id=$userId")->result();
		return $sum[0]->total;
	}
	
	function createNewBetEntity(){
		$ci=& get_instance();
		$ci->db->set('result', '');
		$ci->db->insert(TBL_BET_TOURNAMENT);
		
		return $ci->db->insert_id();
	}
	
	function getListUserBetEvent($event_id){
		$ci=& get_instance();
		return $ci->db->where('event_id', $event_id)->order_by('id','asc')->get(TBL_BET_TOURNAMENT)->result();
	} 
	
	function processUserBetResult(){
		$ci=& get_instance();
		$USER = $ci->user_entity->init('id',$this->user_id);
		$user_tournament = $ci->user_tournament_entity->findUserInTournament($this->user_id, $this->tournament_id);
		$USER_TOURNAMENT = $ci->user_tournament_entity->init('id',$user_tournament->id);
		
		switch($this->result){
			case 'won':
				$pointGet = round( (float)( $this->getOdds() * $this->getStake() ), 2 );
				$USER_TOURNAMENT->setPoint($USER_TOURNAMENT->getPoint() + $pointGet);
				$USER_TOURNAMENT->update();
				
				$this->setPointGet($pointGet);
				$this->update();
				break;
			case 'lost':
				break;
			case 'cancelled':
				$USER->setCoin($USER->getCoin() + $this->getStake());
				$USER->update();
				break;
			case 'pending':
				break;
		}
	}
	
	function getBetHistoryItemsByUserId($user_id){
		$ci=& get_instance();
		return $ci->db->where('user_id',$user_id)->order_by('bet_time','desc')->get(TBL_BET_TOURNAMENT)->result();
	}
	
	function getBetHistoryMatch($user_id, $tournament_id){
		$ci=& get_instance();
		return $ci->db->where( array( 'user_id'=>$user_id, 'tournament_id'=>$tournament_id ) )
		->order_by('bet_time','desc')->get(TBL_BET_TOURNAMENT)->result();
	}
	
	function getBetHistoryItems($user_id, $tournament_id){
		$ci=& get_instance();
		return $ci->db->where( array( 'user_id'=>$user_id, 'tournament_id'=>$tournament_id ) )
		->order_by('bet_time','desc')->get(TBL_BET_TOURNAMENT)->result();
	}
	
	function getMyBetMatch($user_id){
		$ci=& get_instance();
		return $ci->db->where( array( 'user_id'=>$user_id ) )
		->order_by('bet_time','desc')->get(TBL_BET_TOURNAMENT)->result();
	}
	
	//endclass
}
